////////////////////////////////////////////////////////////////////////////////
/// @file    lcdtest.c
/// @author  AE TEAM
/// @brief   THIS FILE PROVIDES ALL THE SYSTEM FUNCTIONS.
////////////////////////////////////////////////////////////////////////////////
/// @attention
///
/// THE EXISTING FIRMWARE IS ONLY FOR REFERENCE, WHICH IS DESIGNED TO PROVIDE
/// CUSTOMERS WITH CODING INFORMATION ABOUT THEIR PRODUCTS SO THEY CAN SAVE
/// TIME. THEREFORE, MINDMOTION SHALL NOT BE LIABLE FOR ANY DIRECT, INDIRECT OR
/// CONSEQUENTIAL DAMAGES ABOUT ANY CLAIMS ARISING OUT OF THE CONTENT OF SUCH
/// HARDWARE AND/OR THE USE OF THE CODING INFORMATION CONTAINED HEREIN IN
/// CONNECTION WITH PRODUCTS MADE BY CUSTOMERS.
///
/// <H2><CENTER>&COPY; COPYRIGHT MINDMOTION </CENTER></H2>
////////////////////////////////////////////////////////////////////////////////

// Define to prevent recursive inclusion
#define _LCDTEST_C_

// Files includes
#include "hal_conf.h"
#include "stdio.h"
#include "delay.h"
#include "lcd.h"

#define LCD_TEST_BUTTON_BOARD
#ifdef LCD_TEST_BUTTON_BOARD
#define LCD_NRST_Port       GPIOE
#define LCD_NRST_Pin        GPIO_Pin_4
#define LCD_NRST_CLK        RCC_AHBENR_GPIOE

#define LCD_BLCTR_Port      GPIOE
#define LCD_BLCTR_Port_Pin  GPIO_Pin_3
#define LCD_BLCTR_CLK       RCC_AHBENR_GPIOE
#endif



void LCD_nRST_Pin_Config(void);
////////////////////////////////////////////////////////////////////////////////
/// @addtogroup MM32_Example_Layer
/// @{

////////////////////////////////////////////////////////////////////////////////
/// @addtogroup LCDTEST
/// @{

////////////////////////////////////////////////////////////////////////////////
/// @addtogroup LCDTEST_Exported_Constants
/// @{


extern void LCD_WR_REG(u16 regval);
extern void LCD_WR_DATA(u16 data);

u16 color1;
////////////////////////////////////////////////////////////////////////////////
/// @brief  lcd display
/// @param  None.
/// @retval None.
////////////////////////////////////////////////////////////////////////////////
int lcd_demo(void)
{
    u8 x = 0;
    while(1) {
        switch(x) {
            case 0:
                LCD_Clear(WHITE);
                break;
            case 1:
                LCD_Clear(BLACK);
                break;
            case 2:
                LCD_Clear(BLUE);
                break;
            case 3:
                LCD_Clear(RED);
                break;
            case 4:
                LCD_Clear(MAGENTA);
                break;
            case 5:
                LCD_Clear(GREEN);
                break;
            case 6:
                LCD_Clear(CYAN);
                break;
            case 7:
                LCD_Clear(YELLOW);
                break;
            case 8:
                LCD_Clear(BRRED);
                break;
            case 9:
                LCD_Clear(GRAY);
                break;
            case 10:
                LCD_Clear(LGRAY);
                break;
            case 11:
                LCD_Clear(BROWN);
                break;
        }
        POINT_COLOR = RED;
        LCD_ShowString(30, 40, 200, 24, 24, (u8*)"MindMotion");
        LCD_ShowString(30, 70, 200, 16, 16, (u8*)"TFTLCD TEST");
        LCD_ShowString(30, 90, 200, 16, 16, (u8*)"@MindMotion");
        x++;
        if(x == 12)x = 0;
        color1 = LCD_ReadPoint(0, 0);
        DELAY_Ms(500);

    }
}

////////////////////////////////////////////////////////////////////////////////
/// @brief  FSMC_GPIO congfiguration
/// @param  None
/// @retval None.
////////////////////////////////////////////////////////////////////////////////
void LCD_PIN_GPIO_Config(void)
{
    GPIO_InitTypeDef GPIO_InitStructure;
    /*

    PD14    FMC_D0
    PD15    FMC_D1
    PD0     FMC_D2
    PD1     FMC_D3
    PE7     FMC_D4
    PE8     FMC_D5
    PE9     FMC_D6
    PE10    FMC_D7
    PE11    FMC_D8
    PE12    FMC_D9
    PE13    FMC_D10
    PE14    FMC_D11
    PE15    FMC_D12
    PD8     FMC_D13
    PD9     FMC_D14
    PD10    FMC_D15
    PB7     FMC_NADV
    PE0     FMC_NBL0
    PE1     FMC_NBL1
    PD7     FMC_NE1
    PG9     FMC_NE2
    PG10    FMC_NE3
    PG12    FMC_NE4
    PD4     FMC_NOE
    PD6     FMC_NWAIT
    PD5     FMC_NWE
        */
    RCC_AHBPeriphClockCmd(RCC_AHBENR_GPIOD | RCC_AHBENR_GPIOE, ENABLE);//RCC_AHBENR_GPIOA | RCC_AHBENR_GPIOB | RCC_AHBENR_GPIOC |

    RCC_APB2PeriphClockCmd(RCC_APB2ENR_SYSCFG, ENABLE);                         //enable sys_cfg clk

    // GPIO_PinAFConfig(GPIOB, GPIO_PinSource7, GPIO_AF_12);       //PB7    FMC_NADV

    GPIO_PinAFConfig(GPIOD, GPIO_PinSource0, GPIO_AF_12);       //PD0   FMC_D2   LCD_D2
    GPIO_PinAFConfig(GPIOD, GPIO_PinSource1, GPIO_AF_12);       //PD1   FMC_D3   LCD_D3
    // GPIO_PinAFConfig(GPIOD, GPIO_PinSource2, GPIO_AF_12);       //
    // GPIO_PinAFConfig(GPIOD, GPIO_PinSource3, GPIO_AF_12);       //
    GPIO_PinAFConfig(GPIOD, GPIO_PinSource4, GPIO_AF_12);       //PD4   FMC_NOE  LCD_RD
    GPIO_PinAFConfig(GPIOD, GPIO_PinSource5, GPIO_AF_12);       //PD5   FMC_NWE  LCD_WR
    // GPIO_PinAFConfig(GPIOD, GPIO_PinSource6, GPIO_AF_12);       //
    GPIO_PinAFConfig(GPIOD, GPIO_PinSource7, GPIO_AF_12);       //PD7   FMC_NE1  LCD_CS
    GPIO_PinAFConfig(GPIOD, GPIO_PinSource8, GPIO_AF_12);       //PD8   FMC_D13  LCD_D13
    GPIO_PinAFConfig(GPIOD, GPIO_PinSource9, GPIO_AF_12);       //PD9   FMC_D14  LCD_D14
    GPIO_PinAFConfig(GPIOD, GPIO_PinSource10, GPIO_AF_12);      //PD10  FMC_D15  LCD_D15
    // GPIO_PinAFConfig(GPIOD, GPIO_PinSource11, GPIO_AF_12);      //
    // GPIO_PinAFConfig(GPIOD, GPIO_PinSource12, GPIO_AF_12);      //
    GPIO_PinAFConfig(GPIOD, GPIO_PinSource13, GPIO_AF_12);      //PD13  FMC_A18  LCD_RS Command or Data
    GPIO_PinAFConfig(GPIOD, GPIO_PinSource14, GPIO_AF_12);      //PD14  FMC_D0   LCD_D0
    GPIO_PinAFConfig(GPIOD, GPIO_PinSource15, GPIO_AF_12);      //PD15  FMC_D1   LCD_D1

    // GPIO_PinAFConfig(GPIOE, GPIO_PinSource0, GPIO_AF_12);
    // GPIO_PinAFConfig(GPIOE, GPIO_PinSource1, GPIO_AF_12);
    // GPIO_PinAFConfig(GPIOE, GPIO_PinSource2, GPIO_AF_12);
    // GPIO_PinAFConfig(GPIOE, GPIO_PinSource3, GPIO_AF_12);
    // GPIO_PinAFConfig(GPIOE, GPIO_PinSource4, GPIO_AF_12);
    // GPIO_PinAFConfig(GPIOE, GPIO_PinSource5, GPIO_AF_12);
    // GPIO_PinAFConfig(GPIOE, GPIO_PinSource6, GPIO_AF_12);
    GPIO_PinAFConfig(GPIOE, GPIO_PinSource7, GPIO_AF_12);       //PE7   FMC_D4   LCD_D4
    GPIO_PinAFConfig(GPIOE, GPIO_PinSource8, GPIO_AF_12);       //PE8   FMC_D5   LCD_D5
    GPIO_PinAFConfig(GPIOE, GPIO_PinSource9, GPIO_AF_12);       //PE9   FMC_D6   LCD_D6
    GPIO_PinAFConfig(GPIOE, GPIO_PinSource10, GPIO_AF_12);      //PE10  FMC_D7   LCD_D7
    GPIO_PinAFConfig(GPIOE, GPIO_PinSource11, GPIO_AF_12);      //PE11  FMC_D8   LCD_D8
    GPIO_PinAFConfig(GPIOE, GPIO_PinSource12, GPIO_AF_12);      //PE12  FMC_D9   LCD_D9
    GPIO_PinAFConfig(GPIOE, GPIO_PinSource13, GPIO_AF_12);      //PE13  FMC_D10  LCD_D10
    GPIO_PinAFConfig(GPIOE, GPIO_PinSource14, GPIO_AF_12);      //PE14  FMC_D11  LCD_D11
    GPIO_PinAFConfig(GPIOE, GPIO_PinSource15, GPIO_AF_12);      //PE15  FMC_D12  LCD_D12

    //GPIO_InitStructure.GPIO_Pin  =  GPIO_Pin_All;
    GPIO_InitStructure.GPIO_Pin  =  GPIO_Pin_0  | \
                                    GPIO_Pin_1  | \
                                    GPIO_Pin_4  | \
                                    GPIO_Pin_5  | \
                                    GPIO_Pin_7  | \
                                    GPIO_Pin_8  | \
                                    GPIO_Pin_9  | \
                                    GPIO_Pin_10 | \
                                    GPIO_Pin_13 | \
                                    GPIO_Pin_14 | \
                                    GPIO_Pin_15;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
    GPIO_Init(GPIOD, &GPIO_InitStructure);

    //GPIO_InitStructure.GPIO_Pin  =  GPIO_Pin_All;
    GPIO_InitStructure.GPIO_Pin  =  GPIO_Pin_7  | \
                                    GPIO_Pin_8  | \
                                    GPIO_Pin_9  | \
                                    GPIO_Pin_10 | \
                                    GPIO_Pin_11 | \
                                    GPIO_Pin_12 | \
                                    GPIO_Pin_13 | \
                                    GPIO_Pin_14 | \
                                    GPIO_Pin_15;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
    GPIO_Init(GPIOE, &GPIO_InitStructure);

}
#define SYSCLK96MPLL//SYSCLK8MHSI
////////////////////////////////////////////////////////////////////////////////
/// @brief  Initialize FSMC
/// @param  None
/// @retval None.
////////////////////////////////////////////////////////////////////////////////
void FSMC_Init_8080(void)
{
    FSMC_InitTypeDef  FSMC_InitStructure;
    FSMC_NORSRAM_Bank_InitTypeDef  FSMC_BankInitStructure;

    FSMC_NORSRAM_BankStructInit(&FSMC_BankInitStructure);
    FSMC_NORSRAMStructInit(&FSMC_InitStructure);

    RCC_AHB3PeriphClockCmd(RCC_AHB3ENR_FSMC, ENABLE);                           //enable FSMCEN
#if defined(SYSCLK8MHSI) //// for 8M HSI OK
    FSMC_BankInitStructure.FSMC_SMReadPipe    = 0;
    FSMC_BankInitStructure.FSMC_ReadyMode     = 0;
    FSMC_BankInitStructure.FSMC_WritePeriod   = 0x2;
    FSMC_BankInitStructure.FSMC_WriteHoldTime = 1;
    FSMC_BankInitStructure.FSMC_AddrSetTime   = 3;
    FSMC_BankInitStructure.FSMC_ReadPeriod    = 0x1;
    FSMC_BankInitStructure.FSMC_DataWidth     = FSMC_DataWidth_16bits;
    FSMC_NORSRAM_Bank_Init(&FSMC_BankInitStructure, FSMC_NORSRAM_BANK0);

    FSMC_InitStructure.FSMC_Mode = FSMC_Mode_8080;
    FSMC_InitStructure.FSMC_TimingRegSelect = FSMC_TimingRegSelect_0;
    FSMC_InitStructure.FSMC_MemSize = FSMC_MemSize_64MB;
    FSMC_InitStructure.FSMC_MemType = FSMC_MemType_NorSRAM;
    FSMC_InitStructure.FSMC_AddrDataMode = FSMC_AddrDataMUX;
    FSMC_NORSRAMInit(&FSMC_InitStructure);
#endif
#if defined(SYSCLK96MPLL) //// for 96M HSI OK
    FSMC_BankInitStructure.FSMC_SMReadPipe    = 0;
    FSMC_BankInitStructure.FSMC_ReadyMode     = 0;
    FSMC_BankInitStructure.FSMC_WritePeriod   = 0x2;
    FSMC_BankInitStructure.FSMC_WriteHoldTime = 1;
    FSMC_BankInitStructure.FSMC_AddrSetTime   = 3;
    FSMC_BankInitStructure.FSMC_ReadPeriod    = 0x1;
    FSMC_BankInitStructure.FSMC_DataWidth     = FSMC_DataWidth_16bits;
    FSMC_NORSRAM_Bank_Init(&FSMC_BankInitStructure, FSMC_NORSRAM_BANK0);

    FSMC_InitStructure.FSMC_Mode = FSMC_Mode_8080;
    FSMC_InitStructure.FSMC_TimingRegSelect = FSMC_TimingRegSelect_0;
    FSMC_InitStructure.FSMC_MemSize = FSMC_MemSize_64MB;
    FSMC_InitStructure.FSMC_MemType = FSMC_MemType_NorSRAM;
    FSMC_InitStructure.FSMC_AddrDataMode = FSMC_AddrDataMUX;
    FSMC_NORSRAMInit(&FSMC_InitStructure);
#endif
}
////////////////////////////////////////////////////////////////////////////////
/// @brief  LCD nReset GPIO configuration
/// @param  None
/// @retval None.
////////////////////////////////////////////////////////////////////////////////
void LCD_nRST_Pin_Config(void)
{
    GPIO_InitTypeDef GPIO_InitStructure;

    //set PC6 as LCD_nRST Pin
    RCC_AHBPeriphClockCmd(LCD_NRST_CLK, ENABLE);
    GPIO_InitStructure.GPIO_Pin = LCD_NRST_Pin;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_Init(LCD_NRST_Port, &GPIO_InitStructure);
}
////////////////////////////////////////////////////////////////////////////////
/// @brief  Reset LCD Controler
/// @param  None
/// @retval None.
////////////////////////////////////////////////////////////////////////////////
void LCD_nRST_Setting(void)
{
    GPIO_SetBits(GPIOE, LCD_NRST_Pin);
    DELAY_Ms(1);
    GPIO_ResetBits(GPIOE, LCD_NRST_Pin);
    DELAY_Ms(5);
    GPIO_SetBits(GPIOE, LCD_NRST_Pin);
    DELAY_Ms(120);
}

////////////////////////////////////////////////////////////////////////////////
/// @brief  LCD backlight GPIO configuration
/// @param  None
/// @retval None.
////////////////////////////////////////////////////////////////////////////////
void LCD_BackLight_Pin_Config(void)
{
    GPIO_InitTypeDef GPIO_InitStructure;

    //set PF11 as LCD Backlight Pin
    RCC_AHBPeriphClockCmd(LCD_BLCTR_CLK, ENABLE);
    GPIO_InitStructure.GPIO_Pin = LCD_BLCTR_Port_Pin;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_Init(LCD_BLCTR_Port, &GPIO_InitStructure);
}
////////////////////////////////////////////////////////////////////////////////
/// @brief  LCD BackLight On
/// @param  None
/// @retval None.
////////////////////////////////////////////////////////////////////////////////
void LCD_BackLight_On(void)
{
    GPIO_SetBits(LCD_BLCTR_Port, LCD_BLCTR_Port_Pin);
}
////////////////////////////////////////////////////////////////////////////////
/// @brief  LCD BackLight Off
/// @param  None
/// @retval None.
////////////////////////////////////////////////////////////////////////////////
void LCD_BackLight_Off(void)
{
    GPIO_ResetBits(LCD_BLCTR_Port, LCD_BLCTR_Port_Pin);
}

////////////////////////////////////////////////////////////////////////////////
/// @brief  FSMC support 8080 interface TFTLCD demo.
/// @param  None.
/// @retval None.
////////////////////////////////////////////////////////////////////////////////
void FSMC_Ex8080TFTLCD_Demo(void)
{

    LCD_nRST_Pin_Config();
    LCD_PIN_GPIO_Config();
    LCD_BackLight_Pin_Config();

    LCD_nRST_Setting();
    FSMC_Init_8080();
    LCD_BackLight_Off();
    LCD_BackLight_On();

    DELAY_Ms(50);                   // delay 50 ms

    LCD_Init();

    POINT_COLOR = RED; //Set the font to red
    lcd_demo();


}

/// @}

/// @}

/// @}
